var sMax = 5;	// Is the maximum number of stars
var rated;
var xmlhttp;
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
} 
function showRating(point)
{
	document.getElementById('rateMe').style.display = "block";
	loadRate(point);
}

function loadRate(grade)
{
	if(grade > 0)
	{
		rating(grade);
		rated = 1;
	}
}

function rating(num){
	
	if(!rated)
	{
		s = num;
		for(i=1; i<=sMax; i++)
		{		
			if(i<=s)
			{
				document.getElementById("_"+i).className = "on";
			}			
		}
	}
}

function off(num){
	if(!rated)
	{
			for(i=1; i<=sMax; i++)
			{		
				document.getElementById("_"+i).className = "";
			}	
	}
}

function rateIt(num){
	if(!rated){		
		rated=1;
		sendRate(num);
	}
}

function sendRate(point){
	alert("Bạn vừa đánh giá ebook này: " + point +" điểm");
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		alert ("Your browser does not support XML HTTP Request");
		return;
	}
	var userId = document.getElementById('user').value;
	var ebookId = document.getElementById('ebook').value;
	var url = "../user/rating.php?user=" + userId + "&ebook=" + ebookId + "&point=" + point;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=sendRateStateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function sendRateStateChanged()
{
	if (xmlhttp.readyState==4 && xmlhttp.status == 200)
	{
		document.getElementById('ratingstar').innerHTML = xmlhttp.responseText;
	}	
}
